﻿using Microsoft.Maui.Storage;
using CarouselView.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarouselView.Services
{
    public class WebClientDownload : IDownloadContract
    {
        private IDirectoryContract _directoryContract;
        public WebClientDownload(IDirectoryContract directoryContract)
        {
            _directoryContract = directoryContract;
        }

        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="original"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public async Task Download(string original, string target)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                using (var fileStream = new FileStream(target, FileMode.Create))
                {
                    var netStream = await httpClient.GetStreamAsync(original);
                    await netStream.CopyToAsync(fileStream);
                }
            }

            _directoryContract.SendBroadcast(target);
        }

    }
}
